package com.example.hello;

import com.example.hello.dao.wechat.UserInfDao;
import com.example.hello.utils.dataSourceUtil.DataSourceContextHolder;
import com.example.hello.utils.dataSourceUtil.DataSourceEnum;
import com.example.hello.utils.dataSourceUtil.DataSourceTypeSheet;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Controller
@MapperScan(basePackages = "com.example.hello.dao.wechat")
public class HelloController {
    private String myValue;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private UserInfDao userInfDao;

    @RequestMapping(path = "/")
//    @GetMapping(path = "/test",params = "myParam=myValue")
    @ResponseBody
    public Object index(){
        /**切换数据库连接池*/
        DataSourceTypeSheet dataSourceTypeSheet = DataSourceTypeSheet.getInstance();
        Map<String, String> placeCodeMap = dataSourceTypeSheet.getPlaceCode_dataSource();
        String xmlDBName = placeCodeMap.get(DataSourceEnum.wireLess.getValue());
        DataSourceContextHolder.setDbType(xmlDBName);

        UserInf userInf = new UserInf();
        userInf.setName("hkc");
        userInf.setAge(28);
        int insert = userInfDao.insert(userInf);
        System.out.println("myValue: "+ myValue);
        System.out.println(insert);

        return "hello boot";
    }

    @PostMapping("/name/{name}/age/{age}")
    @ResponseBody
    public Object index1(@ModelAttribute UserInf userInf){
        System.out.println(userInf.toString());
        return "index1";
    }

    @PostMapping(path = "/user",consumes = "application/json",produces = "application/json;charset=utf-8")
    @ResponseBody
    public UserInf index2(@RequestBody UserInf userInf){
        System.out.println(userInf.toString());
        return userInf;
    }

    /***
     * 没搞懂怎么用
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }

}
